﻿using Lhz.Common.Business;
using Lhz.Common.Interface;
using Lhz.Data;
using Lhz.Data.Linq.Nh;
using System.Collections.Generic;

namespace Lhz.Common.Service
{
    public abstract class EntityService : IEntityService
    {

    }

    public abstract class EntityService<TProxy, TPk, TEntity> : EntityService, IEntityService<TPk, TEntity> where TProxy : EntityProxy<TPk, TEntity> where TEntity : class, new()
    {
        protected TProxy entityProxy;

        public EntityService(TProxy entityProxy)
        {
            this.entityProxy = entityProxy;
        }

        public List<TEntity> Query(string[] paths)
        {
            return entityProxy.Query().LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        public List<TEntity> QueryByPagination(ref Pagination pagination, string[] paths)
        {
            return entityProxy.Query(pagination).LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        public List<TEntity> QueryByOrderBies(List<OrderBy> orderBies, string[] paths)
        {
            return entityProxy.Query(orderBies).LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        public List<TEntity> QueryByPaginationAndOrderBies(ref Pagination pagination, List<OrderBy> orderBies, string[] paths)
        {
            return entityProxy.Query(pagination, orderBies).LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        public TEntity QueryByPk(TPk pk, string[] paths)
        {
            return entityProxy.QueryByPk(pk).LoadByPathsAndClearProxyAndRestoreProxyData(paths);
        }

        public void Add(TEntity item)
        {
            entityProxy.Add(item);
        }

        public void Adds(IEnumerable<TEntity> items)
        {
            entityProxy.Add(items);
        }

        public void Update(TEntity item)
        {
            entityProxy.Update(item);
        }

        public void Updates(IEnumerable<TEntity> items)
        {
            entityProxy.Update(items);
        }

        public void Delete(TEntity item)
        {
            entityProxy.Delete(item);
        }

        public void Deletes(IEnumerable<TEntity> items)
        {
            entityProxy.Delete(items);
        }

        public void DeleteByPk(TPk pk)
        {
            entityProxy.DeleteByPk(pk);
        }

        public void DeleteByPks(IEnumerable<TPk> pks)
        {
            entityProxy.DeleteByPk(pks);
        }
    }
}
